package com.isdepci.project.factor.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.isdepci.common.utils.DateUtils;
import com.isdepci.common.utils.StringUtils;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.factor.domain.CategorizeInfo;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.factor.mapper.ProductCategorizeMapper;
import com.isdepci.project.factor.domain.ProductCategorize;
import com.isdepci.project.factor.service.IProductCategorizeService;

/**
 * 产品分类Service业务层处理
 *
 * @author isdepci
 * @date 2024-11-05
 */
@Service
public class ProductCategorizeServiceImpl implements IProductCategorizeService {
    @Autowired
    private ProductCategorizeMapper productCategorizeMapper;

    /**
     * 查询产品分类
     */
    @Override
    public ProductCategorize selectProductCategorizeById(Long id) {
        return productCategorizeMapper.selectProductCategorizeById(id);
    }

    /**
     * 查询产品分类列表
     */
    @Override
    public List<ProductCategorize> selectProductCategorizeList(ProductCategorize productCategorize) {
        return productCategorizeMapper.selectProductCategorizeList(productCategorize);
    }

    /**
     * 新增产品分类
     */
    @Override
    public AjaxResult insertProductCategorize(ProductCategorize productCategorize) {
        List<ProductCategorize> oldList = productCategorizeMapper.selectCategorizeByCodeLevel(productCategorize.getCode(),productCategorize.getLevel());
//                .stream().filter(v-> productCategorize.getParentCode().equals(v.getParentCode())).collect(Collectors.toList());
        if(oldList.size()>0){
            return AjaxResult.error("当前层级分类已包含相同编码的分类！");
        }
        if(!"0".equals(productCategorize.getParentCode())){
            ProductCategorize info = productCategorizeMapper.selectCategorizeByCodeLevel(productCategorize.getParentCode(),productCategorize.getLevel()-1).stream().findFirst().orElse(null);
            if(info==null){
                return AjaxResult.error("未找到上级分类相关信息！");
            }
            if (info.getStatus() != 1) {
                return AjaxResult.error("当前分类停用，不允许新增");
            }
            productCategorize.setAncestors(info.getAncestors() + "," + productCategorize.getParentCode());
        }else {
            productCategorize.setAncestors("0");
        }
        productCategorize.setCreateUser();
        int row =  productCategorizeMapper.insertProductCategorize(productCategorize);
        if (row > 0) {
            FactorRedisUtils.clearProductCategorizeListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改产品分类
     */
    @Override
    public AjaxResult updateProductCategorize(ProductCategorize productCategorize) {
        productCategorize.setUpdateUser();
        int row = productCategorizeMapper.updateProductCategorize(productCategorize);
        if (row > 0) {
            FactorRedisUtils.clearProductCategorizeListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 删除产品分类信息
     */
    @Override
    public AjaxResult deleteProductCategorizeById(Long id) {
        ProductCategorize old = productCategorizeMapper.selectProductCategorizeById(id);
        if(old==null){
            return AjaxResult.success();
        }
        List<ProductCategorize> children = productCategorizeMapper.selectChildrenByCodeLevel(old.getCode(),old.getLevel()+1);
        if (children.size()>0) {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        int row = productCategorizeMapper.deleteProductCategorizeById(id);
        if (row > 0) {
            FactorRedisUtils.clearProductCategorizeListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
